<!--
 * @Author: your name
 * @Date: 2020-05-19 14:54:42
 * @LastEditTime: 2020-06-04 09:32:41
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \mobile_project_0519\mobile_post\src\pages\user\user.vue
--> 
<template>
  <div class="user">
    <template v-if="token">
      <div class="user-poster">
        <p>欢迎,{{userInfo.name}}</p>
      </div>
      <van-row class="user-links">
        <van-col span="6">
          <van-icon name="pending-payment" />待付款
        </van-col>
        <van-col span="6">
          <van-icon name="records" />待接单
        </van-col>
        <van-col span="6">
          <van-icon name="tosend" />待发货
        </van-col>
        <van-col span="6">
          <van-icon name="logistics" />已收货
        </van-col>
      </van-row>

      <van-cell-group class="user-group">
        <van-cell icon="records" title="地址管理" is-link to="/address" />
        <van-cell icon="points" title="我的积分" is-link />
        <van-cell icon="gold-coin-o" title="我的优惠券" is-link />
        <van-cell icon="gift-o" title="我收到的礼物" is-link />
      </van-cell-group>

      <van-button type="danger" size="large" @click="logout">退出帐号</van-button>
    </template>

    <template v-else>
      <div class="noUserInfo">
        <van-button type="primary" size="small" to="/register">注册</van-button>
        <p>
          已有帐号？
          <router-link to="/login">登录</router-link>
        </p>
      </div>
    </template>
    <tab-bar></tab-bar>
  </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const { mapState, mapMutations } = createNamespacedHelpers("userModule");
//import * as api from "@/request/api/user.js"
export default {
  data() {
    return {
      //userInfo: localStorage.getItem("showToken") || "",
      userInfo:JSON.parse(localStorage.getItem("getUser")) || ""
    };
  },
   computed: {
    ...mapState(["token"]),
    //...mapState(["name"])
  },
  methods: {
    //获取登录用户数据
    // async API_getInfo(){
    //   let res = await api.userInfo()
    //   return res 
    // },
    
    ...mapMutations(["REMOVE_TOKEN"]),
    // 退出登录
    logout() {
      this.REMOVE_TOKEN()
      // this.userInfo = ""
      localStorage.removeItem("getUser")
      this.$toast.success("退出成功！")
    }
  },
  async created(){
    
    // let res = await this.API_getInfo()
    // if(res.data.code === 1000){
    //   this.userInfo = res.data.data
    // }
    // 接收login页提交的用户信息
    this.$eventBus.$on("getUser",(res)=>{
      //console.log(res)
      this.userInfo = JSON.parse(res)
    })

  }
 
};
</script>

<style lang="scss">
@import "@/style/user/user.scss";
</style>